<?php
class Car extends CI_Controller{
	private $data;
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn()) redirect("/");
		$this->load->model("model_car");
	}
		
	/*
			=================Function Add Rent Car=================
	*/
	
	public function index(){
		$data['carrent'] = 'y';
		$data['masterinput'] = 'y';
		$data['owner'] = $this->model_car->getOwnerList();
		$data['ownerLsg'] = $this->model_car->getOwnerListLsg();
		$data['title'] = TITLE.' - Sewa Mobil';
		if($this->input->post("btnSave") ){
			
			$hasil = $this->_createNewRent();
			if($hasil) $data['success'] = "Mobil Berhasil Di Sewa";
		}
		$this->load->view("carRent",$data);
	}
	
		
	function _createNewRent(){
		//$this->form_validation->set_message('_checkpassword', 'Current Password is incorrect');
		$this->form_validation->set_message('_checkDateRent', 
							'Tanggal kembali tidak boleh lebih dulu dari tanggal pinjam, anda sedang sakaw?');
		$this->form_validation->set_message('_checkDateCar', 
							'Mobil sudah disewa untuk tanggal itu');
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'nama_pemilik',
                     'label'   => 'Agency',
                     'rules'   => 'trim|required|xss_clean'
                  ),
				/*array(
                     'field'   => 'tanggal_pinjam',
                     'label'   => 'Tanggal Mulai Pembayaran',
                     'rules'   => 'trim|required'
                  ),
				array(
                     'field'   => 'tanggal_kembali',
                     'label'   => 'Tanggal Akhir',
                     'rules'   => 'trim|required|xss_clean|xss_clean|callback__checkDateRent['
					 										.$this->input->post('tanggal_pinjam').']'
                  ),*/
				array(
                     'field'   => 'tarif',
                     'label'   => 'Tarif',
                     'rules'   => 'trim|integer|xss_clean'
                  ),
				array(
                     'field'   => 'harga_sewa',
                     'label'   => 'Harga Sewa',
                     'rules'   => 'trim|integer|xss_clean'
                  ),
				array(
                     'field'   => 'plat_mobil',
                     'label'   => 'Plat Mobil',
                     'rules'   => 'trim|required|xss_clean|callback__checkDateCar['
					 										.$this->input->post('tanggal_pinjam') .']'									
                  ),
				array(
                     'field'   => 'produsen',
                     'label'   => 'Nama Produsen',
                     'rules'   => 'trim||xss_clean'
                  ),
				array(
                     'field'   => 'tipe',
                     'label'   => 'Tipe Mobil',
                     'rules'   => 'trim|xss_clean'
                  ),
				array(
                     'field'   => 'model',
                     'label'   => 'Model mobil',
                     'rules'   => 'trim|xss_clean'
                  ),
               	array(
                     'field'   => 'daya',
                     'label'   => 'Daya Angkut',
                     'rules'   => 'trim|xss_clean'
                  ),
				array(
                     'field'   => 'mesin',
                     'label'   => 'Besar mesin',
                     'rules'   => 'trim|xss_clean'
                  ),
				array(
                     'field'   => 'bensin',
                     'label'   => 'Jenis Bahan Bakar',
                     'rules'   => 'trim|xss_clean'
                  )
			);
		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_car->addRent();
			return true;
		}
	}
	
	function _checkDateRent($kembali, $pinjam){
		$kembali = strtotime($kembali);
		$pinjam = strtotime($pinjam);
		if($pinjam>$kembali){
			return false;
		}
		return true;
	}
	
	function _checkDateCar($platmobil, $tanggal_mulai){
		$benar = 0;
		$tanggal_akhir = $this->input->post('tanggal_kembali');
		$data = $this->model_car->getDate($platmobil);
		if(!$data){
			return true	;
		}else{
			$tanggal_mulai = strtotime($tanggal_mulai);
			$tanggal_akhir = strtotime($tanggal_akhir);	
			foreach ($data as $data1){
				$data1['tgl_mulai'] = strtotime($data1['tgl_mulai']);
				$data1['tgl_akhir'] = strtotime($data1['tgl_akhir']);
				if((($tanggal_mulai < $data1['tgl_mulai'])&&($tanggal_akhir < $data1['tgl_mulai'] ))||
				(($tanggal_mulai > $data1['tgl_akhir'])&&($tanggal_akhir > $data1['tgl_akhir'] )))
				{
				}else{
					$benar += 1;
				}
			}
			if($benar == 0){
				return true;
			}
			else{
				return false;
			}
		}
	}
	//end of rent car
	
	/*
			=================Function Add Leasing Car=================
	*/
	
	public function leasing(){
		$data['carlsg'] = 'y';
		$data['masterinput'] = 'y';
		$data['owner'] = $this->model_car->getOwnerListLsg();
		$data['mdf'] = $this->model_car->getPaket();
		$data['produsen'] = $this->model_car->getProdusen();
		$data['title'] = TITLE.' - Tambah Mobil Melalui Leasing';
		if($this->input->post("btnSave") ){
			
			$hasil = $this->_createNewLsg();
			if($hasil) $data['success'] = "Mobil Berhasil Di Tambahkan";
		}
		$this->load->view("carLsg",$data);
	}
	
		
	function _createNewLsg(){
		// $this->form_validation->set_message('_checkpassword', 'Current Password is incorrect');
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'nama_pemilik',
                     'label'   => 'Agency',
                     'rules'   => 'trim|xss_clean'
                  ),
				array(
                     'field'   => 'tarif',
                     'label'   => 'Tarif',
                     'rules'   => 'trim|integer|xss_clean'
                  ),
				array(
                     'field'   => 'plat_mobil',
                     'label'   => 'Plat Mobil',
                     'rules'   => 'trim|required|xss_clean|callback__checkusername['
					 										.$this->input->post('plat_mobil') .']'									
                  ),
				array(
                     'field'   => 'produsen',
                     'label'   => 'Nama Produsen',
                     'rules'   => 'trim|required|xss_clean'
                  ),
				array(
                     'field'   => 'tipe',
                     'label'   => 'Tipe Mobil',
                     'rules'   => 'trim|xss_clean'
                  ),
				array(
                     'field'   => 'model',
                     'label'   => 'Model mobil',
                     'rules'   => 'trim|xss_clean'
                  ),
               	array(
                     'field'   => 'daya',
                     'label'   => 'Daya Angkut',
                     'rules'   => 'trim|xss_clean'
                  ),
				array(
                     'field'   => 'mesin',
                     'label'   => 'Besar mesin',
                     'rules'   => 'trim|xss_clean'
                  ),
				array(
                     'field'   => 'bensin',
                     'label'   => 'Jenis Bahan Bakar',
                     'rules'   => 'trim|xss_clean'
                  )
			);
		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_car->addLsg();
			return true;
		}
	}
	function _checkusername($str){
		$this->form_validation->set_message('_checkusername', 'Mobil Sudah Diinput');
		
		$unique = $this->model_car->checkunique("car_license",$str);
		return $unique;
	}
	
	//end of leasing car car
	/*
			=================Function View Car=================
	*/
	function view(){
		$data['masterview'] = 'y';
		$data['viewcar'] = 'y';
		$data['title'] = TITLE.' - Lihat Daftar Mobil'; 
		$data['owner'] = $this->model_car->getOwnerList();
		$data['ownerLsg'] = $this->model_car->getOwnerListLsg();
		$data['car'] = $this->model_car->getCarList(); 
		$this->load->view("carview",$data);
	}
	
	function ajaxLoadCarList(){
		
		$id = $_POST['id'];
		$mode = $_POST['mymode'];
		$data['car'] = $this->model_car->ajaxGetCarList($id, $mode);
		$this->load->view("carListView", $data);
	}
	
	
	/*
			=================Function Edit Car=================
	*/
	
	public function edit(){
		$carId = $this->uri->segment(3, 0);
		$sFlag =  $this->uri->segment(4, 0);
		$data['car'] = $this->model_car->getCarDet($carId);
		$data['price'] = $this->model_car->getCarPrice($carId);  
		$data['masterview'] = 'y';
		$data['produsen'] = $this->model_car->getProdusen();
		$data['viewcar'] = 'y';
		$data['id'] = $carId;
		$data['mdf'] = $this->model_car->getPaket();
		$data['owner'] = $this->model_car->getOwnerList();
		$data['ownerLsg'] = $this->model_car->getOwnerListLsg();
		$data['title'] = TITLE.' - Ubah Data Mobil';
		if($this->input->post("btnSave")){
			$hasil = $this->_editCar($carId);
			if($hasil) {
				$data['success'] = "Data Berhasil Di Ubah";
				redirect('/car/edit/' .$carId .'/s', 'refresh');
			}
		}
		if($sFlag!=""){
			$data['success'] = "Data Berhasil Di Ubah";
		}
		$this->load->view("carEdit",$data);
	}
	
	function _editCar($carId){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'nama_pemilik',
                     'label'   => 'Nama Pemilik',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               	array(
                     'field'   => 'nama_mobil',
                     'label'   => 'Nama Mobil',
                     'rules'   => 'trim|xss_clean'
                  ),
				array(
                     'field'   => 'tarif',
                     'label'   => 'Tarif',
                     'rules'   => 'trim|integer|xss_clean'
                  ),
			 	array(
                     'field'   => 'plat_mobil',
                     'label'   => 'Plat Mobil',
                     'rules'   => 'trim|required|xss_clean|callback__checkDateCar['
					 										.$this->input->post('tanggal_pinjam') .']'									
                  )				
			);
		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_car->editRent($carId);
			return true;
		}
	}
	
	/*
			=================Function Delete Car=================
	*/
	function delete(){
		$id = $this->uri->segment(3, 0);
		$this->model_car->delDataCar($id);
		redirect('/car/view');
	}
	function addProdusen(){
		$bookn = $_POST['bookn'];
		
		$this->model_car->addProdusen($bookn);
	}
	
	function ajaxLoadProdusen(){
		$data['produsen']=$this->model_car->getProdusen();
		$this->load->view("carAjaxLoadProdusen", $data);
	}
	
	
}